package com.bbeginner.learn.no1.widget;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.bbeginner.learn.R;

/**
 * 对比 Capture
 * on click & on long click
 * 使用接口与 class 的区别
 * public void onClick(View v) {
 * public boolean onLongClick(View v) {
 */

public class ClickActivity extends AppCompatActivity {

    private Button btn_click;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_click);

        initView();
    }

    private void initView() {
        //Button btn_click = (Button) findViewById(R.id.btn_click);
        btn_click = findViewById(R.id.btn_click);
        btn_click.setOnClickListener(new MyOnClickListener());
        btn_click.setOnLongClickListener(new MyOnLongClickListener());
    }

    class MyOnClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.btn_click) {
                Toast.makeText(ClickActivity.this, "您点击了控件：" + ((TextView) v).getText(), Toast.LENGTH_SHORT).show();
            }
        }
    }

    class MyOnLongClickListener implements View.OnLongClickListener {
        @Override
        public boolean onLongClick(View v) {
            if (v.getId() == R.id.btn_click) {
                Toast.makeText(ClickActivity.this, "您长按了控件：" + ((TextView) v).getText(), Toast.LENGTH_SHORT).show();
            }
            return true;
        }
    }
}